package com.cunzhang.server.controller.admin;

import com.cunzhang.common.result.PageResult;
import com.cunzhang.common.result.Result;
import com.cunzhang.pojo.dto.CategoryDTO;
import com.cunzhang.pojo.dto.CategoryPageQueryDTO;
import com.cunzhang.pojo.entity.Category;
import com.cunzhang.server.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Api("分类相关接口")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 分类分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分类分页查询")
    public Result<PageResult> pageQueryCategory(CategoryPageQueryDTO categoryPageQueryDTO){
        PageResult pageResult = categoryService.pageQueryCategory(categoryPageQueryDTO);

        return Result.success(pageResult);
    }


    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public Result<List<Category>> queryCategoryByType(Integer type){
        List<Category> list = categoryService.queryCategoryByType(type);

        return Result.success(list);
    }

    /**
     * 修改分类
     * @param categoryDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改分类")
    public Result editCategory(@RequestBody CategoryDTO categoryDTO){
        categoryService.editCategory(categoryDTO);

        return Result.success();
    }


    /**
     * 新增分类
     * @param categoryDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增分类")
    public Result addCategory(@RequestBody CategoryDTO categoryDTO){
        categoryService.addCategory(categoryDTO);


        return Result.success();
    }


}
